package com.estronger.shopmanage.gaodemap;

import java.util.ArrayList;
import com.estronger.shopmanage.R;
import java.util.List;

import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.maps.AMap.OnMapLoadedListener;
import com.amap.api.maps.AMap.OnMarkerClickListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;

/**
 * @ClassName: DetinationMarkerAct
 * @Description: TODO 目的地
 * @author 西瓜汁
 * @date 2015-10-8 下午2:24:53
 */
public class MapMarkerAct extends BaseActivity implements
		OnMarkerClickListener, OnMapClickListener, OnMapLoadedListener {

	private MapView mapView;
	private AMap aMap;
	// 有跳动效果的marker对象
	private LatLng latLng, myLatLng, deslatlng;
	private String title, myTitle, destitle;
	private Marker marker2, marker, marker3;// 有跳动效果的marker对象
	String mLatitude, mLongitude;
	private String latitude, longitude, deslatitude, deslongitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_mapmarker);
		// SDIoc.injectView(this);

		// 客人位置经纬�?
		// 客人地址
		mapView = (MapView) findViewById(R.id.mapmarker_map);
		title = getParamValue("departure").toString();
		latitude = getParamValue("dep_latitude").toString();
		longitude = getParamValue("dep_longitude").toString();
		latLng = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));

		// 我的位置经纬�?
		// 我的地址
		myTitle = MyApplication.address;
		mLatitude = MyApplication.latitude + "";
		mLongitude = MyApplication.longitude + "";
		myLatLng = new LatLng(Double.valueOf(mLatitude),
				Double.valueOf(mLongitude));

		// 客人目的�?
		deslatitude = getParamValue("des_latitude").toString();
		deslongitude = getParamValue("des_longitude").toString();
		destitle = getParamValue("destination").toString();
		deslatlng = new LatLng(Double.valueOf(deslatitude),
				Double.valueOf(deslongitude));
		System.out.println("客人目的地" + destitle + deslatlng);
		mapView.onCreate(savedInstanceState);
		if (aMap == null) {
			aMap = mapView.getMap();
		}
		setUpMarker();
		addMarkersToMap();
	}

	private void setUpMarker() {
		aMap.setOnMapClickListener(this);
		aMap.setOnMarkerClickListener(this);
		aMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听�?
	}

	public Object getParamValue(String key) {
		Bundle data = this.getIntent().getExtras();
		if (data != null) {
			return data.get(key);
		} else {
			return null;
		}
	}

	/**
	 * 在地图上添加marker
	 */
	private void addMarkersToMap() {
		MarkerOptions markerOption = new MarkerOptions()
				// .anchor(0.5f, 0.5f)
				.position(myLatLng)
				.title("我的位置")
				.snippet(myTitle)
				.icon(BitmapDescriptorFactory
						.fromResource(R.drawable.position_driver))
				.draggable(true).period(50);
		System.out.println("我的位置" + myTitle + myLatLng);
		MarkerOptions markerOption1 = new MarkerOptions()
				.anchor(0.5f, 0.5f)
				.position(latLng)
				.title("客人位置")
				.snippet(title)
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.position))
				.draggable(true).period(50);
		System.out.println("客人位置" + title + latLng);
		MarkerOptions markerOption2 = new MarkerOptions()
				// .anchor(0.5f, 0.5f)
				.position(deslatlng)
				.title("客人目的地：")
				.snippet(destitle)
				.icon(BitmapDescriptorFactory.fromResource(R.drawable.position))
				.draggable(true).period(50);
		ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>();
		markerOptionlst.add(markerOption);
		markerOptionlst.add(markerOption1);
		markerOptionlst.add(markerOption2);
		List<Marker> markerlst = aMap.addMarkers(markerOptionlst, true);
		marker = markerlst.get(0);
		marker.showInfoWindow();
		marker2 = markerlst.get(1);
		marker2.showInfoWindow();
		marker3 = markerlst.get(2);
		marker3.showInfoWindow();
		aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}

	@Override
	public boolean onMarkerClick(Marker marker) {
		if (marker.equals(marker2)) {
			if (aMap != null) {
				// jumpPoint(marker, latLng);
				marker2.showInfoWindow();
			}
		} else if (marker.equals(this.marker)) {
			if (aMap != null) {
				// jumpPoint(marker, myLatLng);
				marker.showInfoWindow();
			}
		} else if (marker.equals(this.marker3)) {
			if (aMap != null) {
				// jumpPoint(marker, deslatlng);
				marker3.showInfoWindow();
			}
		}
		return false;
	}

	/**
	 * marker点击时跳动一�?
	 */
	public void jumpPoint(final Marker marker, final LatLng latLng) {
		final Handler handler = new Handler();
		final long start = SystemClock.uptimeMillis();
		Projection proj = aMap.getProjection();
		Point startPoint = proj.toScreenLocation(latLng);
		startPoint.offset(0, -100);
		final LatLng startLatLng = proj.fromScreenLocation(startPoint);
		final long duration = 1500;

		final Interpolator interpolator = new BounceInterpolator();
		handler.post(new Runnable() {
			@Override
			public void run() {
				long elapsed = SystemClock.uptimeMillis() - start;
				float t = interpolator.getInterpolation((float) elapsed
						/ duration);
				double lng = t * latLng.longitude + (1 - t)
						* startLatLng.longitude;
				double lat = t * latLng.latitude + (1 - t)
						* startLatLng.latitude;
				marker.setPosition(new LatLng(lat, lng));
				if (t < 1.0) {
					handler.postDelayed(this, 16);
				}
			}
		});
	}

	@Override
	public void onMapClick(LatLng latLng) {
	}

	@Override
	public void onMapLoaded() {
		addMarkersToMap();
		// 设置�?��maker显示在当前可视区域地图中
		LatLngBounds bounds = new LatLngBounds.Builder().include(latLng)
				.include(myLatLng).include(deslatlng).build();
		aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150));
	}
}
